package com.lam.fundamental;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;

public class GalleryActivity extends Activity{

	private Gallery gallery;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gallery);
		
		gallery=(Gallery)findViewById(R.id.gallery);
		Log.i("gal","hiii");
		gallery.setAdapter(new ImageAdapter(this));
		
		gallery.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View v, int positon,
					long id) {
				// TODO Auto-generated method stub
				Toast.makeText(GalleryActivity.this, String.valueOf(positon), Toast.LENGTH_LONG).show();
			}
		});
	}

	public class ImageAdapter extends BaseAdapter{
		int mGalleryBackground;
		private Context mContext;
		
		public ImageAdapter(Context c){
			mContext=c;
			
			TypedArray array =obtainStyledAttributes(R.styleable.Gallery);
			
			mGalleryBackground = array.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0);
			Log.i("adapter","asdfg"+String.valueOf(mGalleryBackground));
			array.recycle();
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return myImageIds.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ImageView imageView=new ImageView(mContext);
			imageView.setImageResource(myImageIds[position]);
			imageView.setScaleType(ImageView.ScaleType.FIT_XY);
			imageView.setLayoutParams(new Gallery.LayoutParams(400, 400));
			imageView.setBackgroundResource(mGalleryBackground);
			
			return imageView;
		}
		
		
		private Integer[] myImageIds={
				R.drawable.ic_launcher,
				R.drawable.pic1,
				R.drawable.pic2,
				R.drawable.pic3,
		};
	}

}
